<template>
  <el-dialog title="编辑角色" :visible="isShowDia" width="width" @close="btnCancel">
    <!-- 表格 -->
    <el-form ref="rForm" :model="roleForm" label-width="80px" :rules="rules">
      <el-form-item label="角色名称" prop="name"> <el-input v-model="roleForm.name" /></el-form-item>
      <el-form-item label="角色描述"> <el-input v-model="roleForm.description" /></el-form-item>
    </el-form>
    <!-- 底部按钮 -->
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleDetail, updateRole, addRole } from '@/api/setting'
export default {
  props: {

    isShowDia: {
      type: Boolean,
      default: false
    }, // 弹窗是否可视
    addDialog: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      roleForm: {
        name: '',
        description: ''
      }, // 角色表单（弹出层）
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 子组件里的方法/编辑角色可用
    async getRoleDetail(id) {
      this.roleForm = await getRoleDetail(id)
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$emit('update:showDialogedit', false)
      this.$refs.rForm.resetFields()// 移除校验
      console.log('cancel')
    },
    async btnOK() {
      try {
        await this.$refs.rForm.validate()
        // 有id就是编辑没有就是新增
        if (this.roleForm.id) {
        // 编辑角色业务
          await updateRole(this.roleForm)
        // const r = await updateRole(this.roleForm)
        // console.log(r, this.roleForm, '&&&&&&&&')
        } else {
        // 新增角色业务
          await addRole(this.roleForm)
        }
        // 重新拉取数据
        this.addDialog()
        // this.$emit('getRoleList')
        this.$message({ message: '提交成功', type: 'success' })
        this.$emit('update:showDialogedit', false)
        console.log('ok')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
